﻿using Direct.IDAL.User.Money;
using DirectSell.Model.Money;
using DirectSell.Model.Query;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.User.Money
{
    /// <summary>
    /// 货币兑换仓储类
    /// </summary>
    public class MoneyTransforRepository:AbsRepository,IMoneyTransforRepository
    {

        #region 初始化数据
        /// <summary>
        /// 奖金
        /// </summary>
        protected RealMoneyRepository realmoney { get { return new RealMoneyRepository(); } }
        /// <summary>
        /// 电子币
        /// </summary>
        protected VirtualMoneyRepository virtualMoney { get { return new VirtualMoneyRepository(); } }

        #endregion

        #region 查奖金余额
        /// <summary>
        /// 查奖金余额
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public decimal GetRealMoney(string usercode)
        {
           return realmoney.QueryBalance(usercode);
        }
        #endregion

        #region 查电子币余额 
        /// <summary>
        /// 查询电子币余额
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public decimal GetVirtualMoney(string usercode)
        {
            return virtualMoney.QueryVirtualMoney(usercode);
        }
        #endregion

        #region 查转换记录
        /// <summary>
        /// 查转换记录
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public async Task<PagedList<MoneyTransfor>> GetRecord(string usercode,int pageindex,int pagesize)
        {
            PageParam model = new PageParam()
            {
                OrderBy = "Id DESC",
                TableName = "MoneyTransfor",
                PageIndex = pageindex,
                PageSize = pagesize,
                StrWhere = $"UserCode='{usercode}'"
            };
            return await base.PageQuery<MoneyTransfor>(model);
        }

        #endregion

        #region 奖金转电子币
        /// <summary>
        /// 奖金转电子币
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public void RealToVirtual(decimal money,string usercode,ref Hashtable hsTable)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE UserInforMations SET ");
            sb.AppendFormat(" RealMoney-={0},VirtualMoney+={0} ",money);
            sb.AppendFormat(" WHERE UserCode='{0}' ",usercode);
            hsTable.Add(sb.ToString(), null);
        }

        #endregion

        #region 写入转换记录表
        /// <summary>
        /// 写入转换记录表
        /// </summary>
        /// <param name="req"></param>
        public void InsertRecord(MoneyTransforReq req,ref Hashtable hstable)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" INSERT INTO MoneyTransfor ");
            sb.Append(" ([UserCode] ");
            sb.Append(" ,[UserName] ");
            sb.Append(" ,[Amount] ");
            sb.Append(" ,[Type] ");
            sb.Append(" ,[CreateTime] ");
            sb.Append(" ,[Remark]) ");
            sb.Append(" VALUES ");
            sb.AppendFormat(" ('{0}' ",req.UserCode);
            sb.AppendFormat(" ,'{0}' ",req.UserName);
            sb.AppendFormat(" ,{0} ",req.Amount);
            sb.AppendFormat(" ,{0} ",req.Type);
            sb.AppendFormat(" ,'{0}' ",req.CreateTime);
            sb.AppendFormat(" ,'{0}') ",req.Remark);
            hstable.Add(sb.ToString(), null);
        }

        #endregion

    }
}
